package com.cennavi.minemapdemo.activity.exlayer;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.Config;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.RippleCircleLayer;
import com.minedata.minemap.overlay.RippleCircleOptions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//光晕圈demo
public class HaloCircleActivity extends AppCompatActivity {
    private MapView mapView;
    private static MineMap mineMap;
    private RippleCircleLayer rippleCircleLayer;
    private Handler mHandler = new Handler();
    private List<LatLng> latLngs = new ArrayList<>();
    private int index;
    private boolean finish = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MinemapAccountManager.getInstance(this, Config.MAP_NIGHT_TOK, Config.OFFLINE_TOK, Config.MAP_NIGHT_ID);

        setContentView(R.layout.activity_main_layer);
        mapView = findViewById(R.id.mapView);
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(final MineMap mineMap) {
                HaloCircleActivity.mineMap = mineMap;
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_NIGHT_ID, new MineMap.OnStyleLoadedListener() {

                    @Override
                    public void onStyleLoaded(String s) {
                        initLatLngs();
                        timeRunable.run();
                    }
                });
                mineMap.setRepaint(true);
                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.setCameraPosition(
                        //41.805629,123.432359   沈阳
                        //34.2419937,108.9418989 西安   108.81292 34.36734
                        //39.897424, 116.356508  北京    116.3564, 39.8974
                        //18.162968，102.591348  老挝万象附近
                        new CameraPosition.Builder()
                                .target(new LatLng(-20.003, -70.8741))//设置相机指向的位置
                                .zoom(3)//设置相机缩放等级
                                .tilt(0)//设置相机的俯视角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                //设置是否显示地图调试信息，默认是不显示
                mineMap.setDebugActive(false);
                mineMap.setOnRippleCircleFinishListener(new MineMap.OnRippleCircleFinishListener() {

                    @Override
                    public void OnRippleCircleFinish() {
                        finish = true;
                    }
                });
            }
        });
    }

    private void initLatLngs() {
        latLngs.add(new LatLng(-20.003, -70.8741));
        latLngs.add(new LatLng(-5.566, -80.879));
        latLngs.add(new LatLng(-14.0935, -76.2968));
        latLngs.add(new LatLng(33.6793, 131.8197));
        latLngs.add(new LatLng(-3.1128, 148.4774));
    }

    //计时器
    private Runnable timeRunable = new Runnable() {
        @Override
        public void run() {
            if (finish) {
                finish = false;

                if (index >= latLngs.size()) {
                    index = 0;
                }

                if (rippleCircleLayer != null) {
                    mineMap.removeLayer(rippleCircleLayer);
                    rippleCircleLayer = null;
                }

                mineMap.animateCamera(CameraUpdateFactory.newCameraPosition(
                        new CameraPosition.Builder()
                                .target(latLngs.get(index))
                                .tilt(0.0)
                                .zoom(5)
                                .build()
                        ),
                        3000, new MineMap.CancelableCallback() {

                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onFinish() {
                                List<RippleCircleOptions.RippleCircleItem> items = new ArrayList<>();
                                RippleCircleOptions.RippleCircleItem item = new RippleCircleOptions.RippleCircleItem();
                                item.setStrokeColor(Color.TRANSPARENT);
                                item.setFillColor(Color.parseColor("#7CFC00"));
                                item.setAlpha(1.0f);
                                item.setCenter(latLngs.get(index));
                                items.add(item);

                                RippleCircleOptions options = new RippleCircleOptions("circlelayer" + index, RippleCircleOptions.TYPE_HALOCIRCLE);
                                options.addAll(items);
                                options.setRefreshInterval(50);
                                options.setDuration(7);
                                options.setStrokeWidth(RippleCircleOptions.WIDTH_NORMAL);
                                options.setMaxZoom(9);
                                options.setMinZoom(3);
                                rippleCircleLayer = mineMap.addLayer(options);
                                index++;
                            }
                        });
            }
            mHandler.postDelayed(this, 1000);
        }
    };

    @Override
    protected void onStart() {
        if (mapView != null) {
            mapView.onStart();
        }
        super.onStart();
    }

    @Override
    protected void onResume() {
        if (mapView != null)
            mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mapView != null)
            mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        if (mapView != null)
            mapView.onStop();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }

        if (mapView != null)
            mapView.onDestroy();
        super.onDestroy();
    }

    public void btnTest(View view) {
        mineMap.snapshot(new MineMap.OnSnapshotReadyCallback() {
            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                //图片存放路径
                String sdCardDir = "/storage/emulated/0/Android/data/com.minedata.minemap/files/";

                try {
                    File dirFile = new File(sdCardDir);
                    if (!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                    //创建日期
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    String sdfDate = sdf.format(date);
                    File file = new File(sdCardDir, "snapshot" + "(" + sdfDate + ")" + ".png");
                    FileOutputStream fos = new FileOutputStream(file);
                    snapshot.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
